const app = getApp()
var douban=app.globalData.doubanUrl;
import utils from '../../utils/util';
const http=utils.http;
const star=utils.star;
Page({
  data:{
      "in_theaters":{},
      "coming_soon":{},
      "top250":{},
  },
  onLoad:function(options){
      var count="?start=0&count=3";
      var inTheaters=douban+"in_theaters"+count;
      var comingSoon=douban+"coming_soon"+count;
      var top250=douban+"top250"+count;
      http(inTheaters,this.handleData,"in_theaters");
      http(comingSoon,this.handleData,"coming_soon");
      http(top250,this.handleData,"top250");
  },
  handleData(res,type){
     var title=res.data.title;
     var subjects=res.data.subjects;
     var movies=[];
     subjects.forEach(ele=>{
         var average=ele.rating.average;
         var stars=star(ele.rating.stars);
         var title=ele.title;
         var imgUrl=ele.images.small;
         var id=ele.id;
         var temp={
             average,
             stars,
             title,
             imgUrl,
             id,
         }
         movies.push(temp);
     })
     var readyData={}
     readyData[type]={
         movies,
         type,
         title
     };
     this.setData(readyData);
  },
  onSearch(){
      wx.navigateTo({
          url: '/pages/movie-search/movie-search'
      })
  }
})
